<?php
namespace Entities\Intelihelp;

use Doctrine\ORM\Query;
use Doctrine\ORM\EntityRepository;

class EquipeRepository extends EntityRepository
{
	public function listar($params = array())
	{
		$qb		= $this->getEntityManager()->createQueryBuilder();
		$qb->select('e')
		->from('Entities\Intelihelp\Equipe', 'e');

		$where	= $qb->expr()->andX();
		$where->add($qb->expr()->eq(1,1));
		foreach ($params as $key => $value)
		{
			$where->add($qb->expr()->eq($key, $value));

		}
		$qb->where($where)
		->orderBy('e.ds_nome');

		return $this->getEntityManager()->createQuery($qb->getDQL());
	}

	public function listCombo()
	{
		$return	= array();
		$lista	= $this->listar()->getResult();
		foreach ($lista as $value)
		{
			$return[$value->get('id_equipe')]	= $value->get('ds_nome');
		}
		return $return;
	}

	public function listComboExecutor()
	{
		$return	= array();
		$lista	= $this->listar(array('e.fl_executor' => 1))->getResult();
		foreach ($lista as $value)
		{
			$return[$value->get('id_equipe')]	= $value->get('ds_nome');
		}
		return $return;
	}

	public function listNaoMembroCombo($equipe)
	{
		$membros	= array(0);
		foreach ($equipe->get('pessoas') as $value)
		{
			$membros[]	= $value->get('id_pessoa');
		}
		$membros	= implode(',', $membros);

		$qb		= $this->getEntityManager()->createQueryBuilder();
		$qb->select('p')
			->from('Entities\Corp\PessoaFisica', 'p')
			->where($qb->expr()->notIn('p.id_pessoa', $membros))
			->orderBy('p.ds_nome');
		$result	= $qb->getQuery()->getResult(Query::HYDRATE_OBJECT);

		$return	= array();
		foreach ($result as $value)
		{
			$return[$value->get('id_pessoa')]	= $value->get('ds_nome');
		}
		return $return;
	}

	public function listMembroCombo($equipe)
	{
		$membros	= array();

		if (is_object($equipe))
		{
			foreach ($equipe->get('pessoas') as $value)
			{
				$membros[$value->get('id_pessoa')]	= $value->get('ds_nome');
			}
		}

		return $membros;
	}
}